﻿/*
 * Created by SharpDevelop.
 * User: Lousabit
 * Date: 28/11/2011
 * Time: 5:42
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Generic;
using System.IO;
using System.Web;
using Business_Logic;
using Business_Entity;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace PetSoft.GUI.Clinica
{
	
	
	
	/// <summary>
	/// Description of GUIReporteProximaCita.
	/// </summary>
	public partial class GUIReporteProximaCita : Form
	{
		
		private BECliente cliente;
		private BEMascota mascota;
		
		public GUIReporteProximaCita()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			cliente = null;
			mascota = null;
			
		}
		
		void DateTimePicker1ValueChanged(object sender, EventArgs e)
		{
			Utils.dateMenorQue(desdeDTP, hastaDTP);
		}
		
		void HastaDTPValueChanged(object sender, EventArgs e)
		{
			Utils.dateMayorQue(hastaDTP, desdeDTP);
		}
		
		void BuscarClienteBTNClick(object sender, EventArgs e)
		{
			try{
				GUI.Ventas.GUIMantenimientoCliente v = new GUI.Ventas.GUIMantenimientoCliente(true);
				v.ShowDialog(this);
				
				if (v.ClienteSeleccionado != null){
					this.clienteTB.Text = v.ClienteSeleccionado.Nombre + " " + v.ClienteSeleccionado.ApellidoPaterno;
					this.cliente = v.ClienteSeleccionado;
				}
			}catch(Exception ex){
				Utils.MessageError("Error al buscar cliente.", ex);
			}
			
		}
		
		
		void BuscarMascotaBTNClick(object sender, System.EventArgs e)
		{
			try{
				if (this.cliente == null){
					Utils.MessageAviso("Primero seleccionar un cliente");
					return;
				}else{
					Ventas.GUICliente v = new Ventas.GUICliente(this.cliente, 3);
					v.ShowDialog(this);
					
					if (v.MascotaSeleccionada != null){
						this.mascota = v.MascotaSeleccionada;
						mascotaTB.Text = this.mascota.Nombre;
					}
				}
			}catch(Exception ex){
				Utils.MessageError("Error al buscar mascota",ex);
			}
		}
		
		void GenerarBTNClick(object sender, System.EventArgs e)
		{
			try{
				if (this.mascota == null){
					Utils.MessageAviso("Primero seleccionar una mascota");
					return;
				}else{
					//GENERAR REPORTE
					
					List<BELineaReporteProximasCitas> lineas = BLReporteProximasCitas.getReporte(mascota.Id, desdeDTP.Value, hastaDTP.Value);
					
					String path;
					
					try{
						Document doc = new Document(PageSize.A4.Rotate());
						path = "..\\ReporteProximasCitas.pdf";
						
						PdfWriter.GetInstance(doc, new FileStream(path, FileMode.Create));
						doc.Open();	
						
						PdfPTable tab = new PdfPTable(5);
						tab.TotalWidth = 820f;
						float[] widths = new float[] { 820/5f, 820/5f, 820/5f, 820/5f, 820/5f};
						tab.SetWidths(widths);
						tab.LockedWidth = true;
						
						PdfPCell cell;
						iTextSharp.text.Image img;
						
						//AQUI VA EL LOGO DE PETSOFT!!!!!!!!!!!////////////////////////
						/////////////////////////////////////////////////////////////
						img = Utils.getEncabezadoReporte();
						cell = new PdfPCell(img, false);
						cell.Colspan = 5;
						cell.Border = 0;
						cell.HorizontalAlignment = 0;
						tab.AddCell(cell);
						//////////////////////////////////////////////////////
						/////////////////////////////////////////////////////////
						
						
						iTextSharp.text.Font fntTit = FontFactory.GetFont(FontFactory.HELVETICA, 16, BaseColor.WHITE);
						iTextSharp.text.Font fntTit2 = FontFactory.GetFont(FontFactory.HELVETICA, 16, BaseColor.BLACK);
						iTextSharp.text.Font fntTab = FontFactory.GetFont(FontFactory.HELVETICA, 8, BaseColor.BLACK);
									
						cell = new PdfPCell(new Phrase("REPORTE DE PRÓXIMAS CITAS",fntTit));
						cell.Colspan = 5;
						cell.Border = 0;
						cell.HorizontalAlignment = 1;
						cell.BackgroundColor = BaseColor.BLACK;
						tab.AddCell(cell);
						
						cell = new PdfPCell(new Phrase("Cita realizada",fntTit2));
						cell.Colspan = 4;
						cell.Border = 0;
						cell.HorizontalAlignment = 1;
						cell.BackgroundColor = BaseColor.LIGHT_GRAY;
						tab.AddCell(cell);
						
						cell = new PdfPCell(new Phrase("Cita próxima",fntTit));
						cell.Colspan = 1;
						cell.Border = 0;
						cell.HorizontalAlignment = 1;
						cell.BackgroundColor = BaseColor.GRAY;
						tab.AddCell(cell);
						
						
						
						cell = new PdfPCell(new Phrase("Fecha",fntTab));
						cell.Colspan = 1;
						cell.BackgroundColor = BaseColor.LIGHT_GRAY;
						cell.Border = 0;
						tab.AddCell(cell);
									
						cell = new PdfPCell(new Phrase("Servicio",fntTab));
						cell.Colspan = 1;
						cell.BackgroundColor = BaseColor.LIGHT_GRAY;
						cell.Border = 0;
						tab.AddCell(cell);
						
						cell = new PdfPCell(new Phrase("Encargado",fntTab));
						cell.Colspan = 1;
						cell.BackgroundColor = BaseColor.LIGHT_GRAY;
						cell.Border = 0;
						tab.AddCell(cell);
						
						cell = new PdfPCell(new Phrase("Tienda",fntTab));
						cell.Colspan = 1;
						cell.BackgroundColor = BaseColor.LIGHT_GRAY;
						cell.Border = 0;
						tab.AddCell(cell);
						
						cell = new PdfPCell(new Phrase("Fecha",fntTab));
						cell.Colspan = 1;
						cell.BackgroundColor = BaseColor.GRAY;
						cell.Border = 0;
						tab.AddCell(cell);
						
						
						foreach(BELineaReporteProximasCitas lr in lineas){
							cell = new PdfPCell(new Phrase(lr.FechaAntigua.ToShortDateString(), fntTab));
							cell.Colspan = 1;
							cell.BackgroundColor = BaseColor.WHITE;
							cell.Border = 0;
							tab.AddCell(cell);
							
							cell = new PdfPCell(new Phrase(lr.Servicio, fntTab));
							cell.Colspan = 1;
							cell.BackgroundColor = BaseColor.WHITE;
							cell.Border = 0;
							tab.AddCell(cell);
							
							cell = new PdfPCell(new Phrase(lr.Encargado, fntTab));
							cell.Colspan = 1;
							cell.BackgroundColor = BaseColor.WHITE;
							cell.Border = 0;
							tab.AddCell(cell);
							
							cell = new PdfPCell(new Phrase(lr.Tienda, fntTab));
							cell.Colspan = 1;
							cell.BackgroundColor = BaseColor.WHITE;
							cell.Border = 0;
							tab.AddCell(cell);
							
							cell = new PdfPCell(new Phrase(lr.Fecha.ToShortDateString(), fntTab));
							cell.Colspan = 1;
							cell.BackgroundColor = BaseColor.WHITE;
							cell.Border = 0;
							tab.AddCell(cell);
						}
						
						
						doc.Add(tab);
						
						doc.Close();
					}catch(Exception ex){
						Utils.MessageError("Error al generar reporte.", ex);
						return;
					}	
					
					try{
						System.Diagnostics.Process.Start("AcroRd32.exe",path);
					}catch(Exception ex){
						Utils.MessageError("No se puede abrir el archivo generado.");
						return;
					}
					
				}
			}catch(Exception ex){
				Utils.MessageError("Error al generar reporte", ex);
			}
		}
		
		
	}
}
